package com.yupi.springbootinit.register;


import com.yupi.springbootinit.enums.SearchKeyWord;
import com.yupi.springbootinit.service.impl.search.PictureSearchService;
import com.yupi.springbootinit.service.impl.search.PostSearchService;
import com.yupi.springbootinit.service.impl.search.SearchService;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class SearchRegisterService {

    @Resource
    private SearchService<?>[] services;

    @Resource
    private PictureSearchService pictureSearchService;

    @Resource
    private PostSearchService postSearchService;


    public final HashMap<SearchKeyWord, SearchService<?>> searchServiceMap = new HashMap<>();


    public SearchService<?> getSearchService(SearchKeyWord keyWord) {
        if (services == null || keyWord == null) return null;
        for (SearchService<?> service : services) {
            SearchKeyWord searchKeyWord = service.getSearchKeyWord();
            if (searchKeyWord == null) continue;
            if (searchKeyWord.equals(keyWord)) return service;
        }
        return null;
    }

	@PostConstruct
    public Map<SearchKeyWord, SearchService<?>> registerService() {
		searchServiceMap.put(SearchKeyWord.POST, postSearchService);
		searchServiceMap.put(SearchKeyWord.PICTURE, pictureSearchService);
        return searchServiceMap;
    }
}
